<?php

require_once(dirname(__FILE__).'/facebook/facebookapi_php5_restlib.php');
require_once(dirname(__FILE__).'/facebook/facebook.php');
require_once(dirname(__FILE__).'/facebook/facebook_desktop.php');

class FacebookUtility {
	public static function get_application_id() {
		return 0;
	}
	
	public static function get_api_key() {
		return "0";
	}
	
	public static function get_api_secret() {
		return "0";
	}
	
	public static function facebook_client() {
		static $facebook = null;
		if ($facebook == null) {
			$facebook = new Facebook(self::get_api_key(), self::get_api_secret());
			if (!$facebook) {
				return null;
			}
		}
		return $facebook;
	}
	
	public static function get_colleges_list($user) {
		return get_colleges_list($user->getFacebookId());
	}
	
	public static function get_colleges_list_by_id($fb_uid) {
		$affiliations = FacebookUtility::get_info($fb_uid, 'affiliations');
		// return print_r(FacebookUtility::get_info($fb_uid, 'affiliations'));
		$result = array();
		$list = $affiliations[0];
		foreach ($affiliations as $affiliation) {
			if ($affiliation['type'] != 'college')
				continue;
			array_push($result, $affiliation);
		}
		return $result;
	}
	
	public static function get_formatted_colleges_list($user) {
		$result = array();
		foreach (get_colleges_list($user) as $affiliation) {
			array_push($result, $affiliation['name'] . " '" . substr($affiliation['year'], -2));
		}
		return $result;
	}
	
//	public static function get_current_user_info($field) {
//		return self::get_info(self::facebook_client()->get_loggedin_user(), $field);
//	}
	
	public static function get_info($id, $field) {
		$data = FacebookUtility::facebook_client()->api_client->users_getInfo($id, $field);
		return $data[0][$field];
	}
	
	public static function publish_note($id, $title, $content) {
		self::facebook_client()->api_client->notes_create($id, $title, $content);
	}
	
//	public static function publish_feed($content) {
//		self::facebook_client()->api_client->feed_publishUserAction(self::get_template_bundle_id(), $content);
//	}
	
	public static function register_feed_form_js($content) {
		$templateData = json_encode(array ('content'=>$content, 'content2'=>"<br /><a href='http://www.finalsclub.org/'>Join FinalsClub!</a>"));
		
		self::onloadRegister(sprintf("facebook_publish_feed(%s); ", $templateData));
	}
	
	public static function render_fbconnect_button($size='medium') {
		return '<fb:login-button '.
	           	'size="'.$size.'" background="light" length="long" '.
	           	'onlogin="facebookConnected();"></fb:login-button>';
	}
	
	public static function has_granted_permission($permission) {
		$result = self::facebook_client()->api_client->users_hasAppPermission($permission);
		if ($result == 1)
			return true;
		if ($result == 2)
			return false;
	}
	
	public static function onloadRegister($js) {
		if (isset($_SESSION['onload_js']))
			$_SESSION['onload_js'] = $onload_js . $js;
		else
			$_SESSION['onload_js'] = $js;
	}
}
?>